package may.qf.controller;

import may.qf.entity.Dept;
import may.qf.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/getAllDept")
    public String getAllDept(Model model) {
        List<Dept> deptList = deptService.queryAll();
        model.addAttribute("deptList", deptList);
        return "dept/deptList";
    }

    @RequestMapping(value = "/addDept" ,method = RequestMethod.POST)
    public String addDept(Dept dept) {
        System.out.println("dept = " + dept);
        int update = deptService.addDept(dept);
        System.out.println("update = " + update);
        return "redirect:getAllDept";
    }

    @RequestMapping("/updateDept")
    public String updateDept(Dept dept) {
        System.out.println("dept = " + dept);
        int update = deptService.updateDept(dept);
        System.out.println("update = " + update);
        return "redirect:getAllDept";
    }

    @RequestMapping("/delDept")
    public String dellDept(Integer deptNo) {
        int update = deptService.delDept(deptNo);
        System.out.println("update = " + update);
        return "redirect:getAllDept";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer deptNo, Model model) {
        Dept dept = deptService.queryDeptByDeptNo(deptNo);
        model.addAttribute("deptInfo", dept);
        return "dept/update";
    }
}
